/**
 * Created with IntelliJ IDEA.
 * User: duoba
 * Date: 16/2/22
 * Time: 下午3:22
 * To change this template use File | Settings | File Templates.
 */

package com.cake21.uitest.System.Map;

import java.io.InputStream;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;

import com.cake21.uitest.System.Core.Config;

public class BrowserMap {
    /*浏览器配置*/
    private static ConcurrentHashMap<String, HashMap<String, String>> map = initMap();

    public static ConcurrentHashMap<String, HashMap<String, String>> getMap() {
        return map;
    }

    /*浏览器配置初始化*/
    private static ConcurrentHashMap<String, HashMap<String, String>> initMap() {

        try {
            // 读取XML文件
            InputStream is = BrowserMap.class.getClassLoader().getResourceAsStream("browsers.xml");
            NodeList browserList = new Config(is).readTags("browser");
            ConcurrentHashMap<String, HashMap<String, String>> browserMap = new ConcurrentHashMap<String, HashMap<String, String>>();
            for (int i = 0; i < browserList.getLength(); i++) {
                // 获取所有浏览器的全部配置信息
                NamedNodeMap browserAttrs = browserList.item(i).getAttributes();
                HashMap<String, String> attrMap = new HashMap<String, String>();
                attrMap.put("name", Config.getNodeString(browserAttrs.getNamedItem("name")));
                attrMap.put("type", Config.getNodeString(browserAttrs.getNamedItem("type")));
                attrMap.put("version", Config.getNodeString(browserAttrs.getNamedItem("version")));
                attrMap.put("platform", Config.getNodeString(browserAttrs.getNamedItem("platform")));
                attrMap.put("pageLoadTimeout", Config.getNodeString(browserAttrs.getNamedItem("pageLoadTimeout")));
                attrMap.put("scriptTimeout", Config.getNodeString(browserAttrs.getNamedItem("scriptTimeout")));
                attrMap.put("implicitlyWait", Config.getNodeString(browserAttrs.getNamedItem("implicitlyWait")));
                attrMap.put("platformtype", Config.getNodeString(browserAttrs.getNamedItem("platformtype")));
                browserMap.put("Browser-" + i, attrMap);
            }
            return browserMap;
        } catch (Exception e) {
            return null;
        }
    }

}
